﻿namespace Ecreate.Data
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.Common;
    public abstract class DBEngine:IDBEngine
    {
        #region 执行事务
        public abstract IDbTransaction BeginTransaction();
        public abstract bool CommitTrans();
        public abstract bool RollBack();
        //批量执行Sql语句
        public bool ExecuteTransactionSql(List<String> list)
        {
            IDbTransaction Trans = null;
            try
            {
                this.DbConnection.Open();
                Trans = DbConnection.BeginTransaction();
                IDbCommand command=this.DbConnection.CreateCommand();
                command.Transaction = Trans;
                for (int i = 0; i < list.Count; i++)
                {
                    command.CommandText = list[i];
                    int ret = command.ExecuteNonQuery();
                }
                Trans.Commit();
                Trans.Dispose();
                return true;
            }
            catch
            {
                if (Trans != null)
                {
                    Trans.Rollback();
                }
                return false;
            }
            finally
            {
                try
                {
                    if (Trans != null)
                    {
                        Trans.Dispose();
                        Trans = null;
                    }
                }
                catch { }
                try
                {
                    if (DbConnection.State != ConnectionState.Closed)
                    {
                        DbConnection.Close();
                    }
                }
                catch 
                { 
                
                
                }
            }
        }
        public bool ExecuteTransactionSql(List<SqlStruct> list)
        {


            return true;
        }
        #endregion
        public abstract DataSet ExecuteDataSet(SqlStruct sqlStruct);
        public abstract DataSet ExecuteDataSet(CommandType commandType, string commandText, QueryParameterCollection commandParameters, DataSet ds, string tableName);
        public abstract DataSet ExecuteDataSet(string commandText, QueryParameterCollection commandParameters, DataSet ds, int startRecord, int maxRecords, string tableName);

        public DataSet ExecuteDataSet(string commandText)
        {
            return this.ExecuteDataSet(CommandType.Text, commandText, null, new DataSet(), null);
        }

        public DataSet ExecuteDataSet(System.Data.CommandType commandType, string commandText)
        {
            return this.ExecuteDataSet(commandType, commandText, null, new DataSet(), null);
        }
        public DataSet ExecuteDataSet(string commandText, Ecreate.Data.QueryParameterCollection ParamterCollection)
        {
            return this.ExecuteDataSet(commandText, ParamterCollection, new DataSet(), null);
        }

        public DataSet ExecuteDataSet(string commandText, DataSet dataSet)
        {
            return this.ExecuteDataSet(CommandType.Text, commandText, null, dataSet, null);
        }
        public DataSet ExecuteDataSet(string commandText, string tableName)
        {
            return this.ExecuteDataSet(CommandType.Text, commandText, null, new DataSet(), tableName);
        }
        public DataSet ExecuteDataSet(System.Data.CommandType commandType, string commandText, Ecreate.Data.QueryParameterCollection ParameterCollection)
        {
            return this.ExecuteDataSet(commandType, commandText, ParameterCollection, new DataSet(), null);
        }
        public DataSet ExecuteDataSet(System.Data.CommandType commandType, string commandText, DataSet dataSet)
        {
            return this.ExecuteDataSet(commandType, commandText, null, dataSet, null);
        }
        public DataSet ExecuteDataSet(System.Data.CommandType commandType,string commandText,string tableName)
        {
            return this.ExecuteDataSet(commandType, commandText, null, new DataSet(), tableName);
        }

        public DataSet ExecuteDataSet(string commandText, Ecreate.Data.QueryParameterCollection ParameterCollection, DataSet dataset)
        {
            return this.ExecuteDataSet(CommandType.Text, commandText, ParameterCollection, dataset, null);
        }
        public DataSet ExecuteDataSet(string commandText, Ecreate.Data.QueryParameterCollection ParameterCollection, string tableName)
        {
            return this.ExecuteDataSet(CommandType.Text, commandText, ParameterCollection, new DataSet(), tableName);
        }
        public DataSet ExecuteDataSet(string commandText, DataSet dataset, string tableName)
        {
            return this.ExecuteDataSet(CommandType.Text, commandText, null, dataset, tableName);
        }

        public DataSet ExecuteDataSet(CommandType commandType, string commandText, QueryParameterCollection ParameterCollection, DataSet dataSet)
        {
            return this.ExecuteDataSet(commandType, commandText, ParameterCollection, dataSet, null);
        }

        public DataSet ExecuteDataSet(CommandType commandType, string commandText, QueryParameterCollection ParameterCollection, string tableName)
        {
            return this.ExecuteDataSet(commandType, commandText, ParameterCollection, new DataSet(), tableName);

        }

        public DataSet ExecuteDataSet(CommandType commandType, string commandText, DataSet dataSet, string tableName)
        {
            return this.ExecuteDataSet(commandType, commandText, null, dataSet, tableName);
        }
        public DataSet ExecuteDataSet(string commandText, QueryParameterCollection ParameterCollection, DataSet dataSet, string tableName)
        {
            return this.ExecuteDataSet(CommandType.Text, commandText, ParameterCollection, dataSet, tableName);
        }
        public abstract DataTable GetDataTable(string commandText);


        public string GetResultString(string commandText)
        {
            DataTable dt = this.GetDataTable(commandText);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString();

            }
            else
            {
                return "";
            }
        }
        public abstract DataTable GetDataSchemaByTableName(string commandText);


        #region 执行Sql语句
        public int ExecuteNonQuery_inner(String commandText, bool inTrans)
        {
            lock (this)
            {
                int ret = -1;
                IDbCommand command=this.DbConnection.CreateCommand();
                command.CommandText = commandText;
                try
                {
                    if (!inTrans)
                    {
                        if (DbConnection.State != ConnectionState.Closed)
                            DbConnection.Close();
                    }
                    else
                    {
                        if (DbConnection.State != ConnectionState.Open)
                        {
                            throw new ApplicationException("数据库连接未打开！");
                        }
                        //command.Transaction = innerTrans;
                    }

                    if (!inTrans)
                        DbConnection.Open();

                    ret = command.ExecuteNonQuery();
                    if (!inTrans)
                        DbConnection.Close();
                    return ret;
                }
                catch 
                {
                    return -1;
                }
                finally
                {
                    if (!inTrans)
                    {
                        if (DbConnection.State == System.Data.ConnectionState.Open)
                        {
                            DbConnection.Close();
                        }
                    }
                }
            }
        }
        public int ExecuteNonQuery(string commandText)
        {
            return ExecuteNonQuery_inner(commandText, false);
        }
        #endregion


        public abstract string GetConnectionString();


        public abstract IDbConnection DbConnection { get; }

        public void Open()
        {
            if (this.DbConnection.State.Equals(ConnectionState.Closed))
            {
                this.DbConnection.Open();
            }
        }
        public void Close()
        {
            this.DbConnection.Close();
        }
        public bool IsClosed 
        {
            get
            {
                return this.DbConnection.State.Equals(ConnectionState.Closed);
            }
        }
        public bool TestConnection()
        {
            return false;
        }


        public abstract Ecreate.Data.DataBaseType DatabaseType { get; }


        protected virtual void GetQueryParameter(DbCommand cmd, QueryParameterCollection commandParameters)
        {
            if ((cmd.Parameters != null) && (cmd.Parameters.Count > 0))
            {
                for (int i = 0; i < cmd.Parameters.Count; i++)
                {
                    commandParameters[i].Value = cmd.Parameters[i].Value;
                }
            }
        }
        private IFace.IService service = null;
        public IFace.IService getService()
        {
            if (this.service == null)
            {
                service = new IFace.ServiceInstance();
            }
            return this.service;
        }



    }
}
